<?php


namespace app\admin\service\config;

use app\admin\model\config\ExchangeConfig;
use app\admin\model\config\SignConfig;
use app\admin\service\BaseService;
use app\admin\service\CommonService;

/**
 * 行为日志-服务类
 * @author tangsan
 * Class ActionLogService
 * @package app\admin\service
 */
class SignConfigService extends BaseService
{
    /**
     * 构造函数
     * @author tangsan
     * ActionLogService constructor.
     */
    public function __construct()
    {
        $this->model = new SignConfig();
    }

    /**
     * 获取数据列表
     * @return array
     * @author tangsan
     */
    public function getList()
    {
//        $param = request()->param();
        // 用户账号
        $sort = 'ItemIndex ASC';
        $result = $this->model
            ->field('ItemIndex,ItemType,ItemCount,Remark')
            ->order($sort)->page(PAGE, PERPAGE)
            ->select()->toArray();
        //获取数据总数
        $count = $this->model->count();

        if ($result) {
            foreach ($result as &$val) {
                $val['ItemCount'] = CommonService::dbToShow($val['ItemCount']);
            }
        }
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $result,
            "count" => $count,
        );
        return $message;
    }


    public function edit()
    {
        $param = request()->param();

        try {
            $ItemIndex = $param['ItemIndex'];
            $ItemType = $param['ItemType'];
            $ItemCount = $param['ItemCount'];
            $Remark = $param['Remark'];
            $error = '';
            if (!in_array($ItemType, [0, 1])) {
                $error = '参数错误！';
            }
            if (!$ItemCount) {
                $error = '参数错误！';
            }
            if (!$Remark) {
                $error = '参数错误！';
            }
            $data['ItemCount'] = CommonService::InsertToDb($ItemCount);;
            $data['ItemType'] = $ItemType;
            $data['Remark'] = $Remark;
            $update = SignConfig::where('ItemIndex', $ItemIndex)->update($data);
            if ($update) {
                return message();
            }
            return message($error, false);
        } catch (\Exception $exception) {
            return message('系统错误！！请联系管理员！！！', false);
        }


    }

    protected static function itemType($id)
    {
        $type = [
            0 => '金币',
            1 => '类型'
        ];
        return isset($type[$id]) ?? '';

    }

}